%%%Askhsh 1
disp('Askhsh 1')
pause;

a = [2 1 1]';
%%dianysma b or8ogwnio me to a
%%to b den einai monadiko.Yparxoun apeira dianysmata or8ogwnia me to a
b = [-3 2 4]';  
drawVector([a b ], 'r.', {'a','b'});
view(-160, 7)


%%%Askhsh 2
disp('Askhsh 2')
pause;
a = [2 1 1]';
b = [-3 2 4]';  
c = cross(a,b)
figure(1); clf;
drawVector([a b c], 'r.', {'a','b','c'});
view(-160, 7)


%%%Askhsh 3
disp('Askhsh 3')
pause;
A = [a b c]; 
X = sampleUnitSphere(3,200);
AX = A*X;
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),AX(3,:),'g.'); 
drawVector(A, 'r.') 
hold off; axis square equal; grid on;


%%%Askhsh 4
disp('Askhsh 4')
pause;
B = [a b c];
C = diag([1/norm(a) 1/norm(b) 1/norm(c)]);
A = B*C;

%%Optikopoihsh ths monadiaias sfairas pou metasxhmatizetai apo to A
disp('Optikopoihsh ths monadiaias sfairas pou metasxhmatizetai apo to A')
pause;
X = sampleUnitSphere(3,200);
AX = A*X;
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),AX(3,:),'g.'); 
drawVector(A, 'r.') 
hold off; axis square equal; grid on;

%%Optikopoihsh ths monadiaias sfairas pou metasxhmatizetai apo to AB
disp('Optikopoihsh ths monadiaias sfairas pou metasxhmatizetai apo to AB')
pause;
X = sampleUnitSphere(3,200);
ABX = (A*B)*X;
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(ABX(1,:),ABX(2,:),ABX(3,:),'g.'); 
drawVector((A*B), 'r.') 
hold off; axis square equal; grid on;

%%Optikopoihsh ths monadiaias sfairas pou metasxhmatizetai apo to AAB
disp('Optikopoihsh ths monadiaias sfairas pou metasxhmatizetai apo to AAB')
pause;
X = sampleUnitSphere(3,200);
AABX = (A*A*B)*X;
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AABX(1,:),AABX(2,:),AABX(3,:),'g.'); 
drawVector((A*A*B), 'r.') 
hold off; axis square equal; grid on;

%%Optikopoihsh ths monadiaias sfairas pou metasxhmatizetai apo to AAAB
disp('Optikopoihsh ths monadiaias sfairas pou metasxhmatizetai apo to AAAB')
pause;
X = sampleUnitSphere(3,200);
AAABX = (A*A*A*B)*X;
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AAABX(1,:),AAABX(2,:),AAABX(3,:),'g.'); 
drawVector((A*A*A*B), 'r.') 
hold off; axis square equal; grid on;


%%%Askhsh 5
disp('Askhsh 5')
pause;
A = [1 -1; 1 1];
X = sampleUnitSphere(2,200);
AX = A*X;
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),'g.'); 
drawVector(A, 'r.') 
hold off; axis
A_inv = inv(A)


%%%Normes Pinakwn
disp('Normes Pinakwn')
pause;

%%%Askhsh 1
 disp('Askhsh 1')
 pause;
 
 A = [-2 3; -3 1 ];
 B = [1 3; 1 1];
 C = reshape(1:4,2,2);
 X = sampleUnitSphere(2, 100); 
 AX = A*X; 
 BX = B*X;
 CX = C*X;
 figure(1); clf; hold on;
 plot( X(1,:), X(2,:),'b.');
 plot(AX(1,:),AX(2,:),'r.');
 plot(BX(1,:),BX(2,:),'r.');
 plot(CX(1,:),CX(2,:),'r.');
 title('Sygrish pinakwn A,B kai C');
 hold off 
 axis square equal
 
 
 %%%Askhsh 2
 disp('Askhsh 2')
 pause
 B = [1 3; 1 1];
 C = -B;
 D = B';
 X = sampleUnitSphere(2, 100);
 BX = B*X; 
 CX = C*X;
 DX = D*X;
 figure(1); clf; hold on;
 plot( X(1,:), X(2,:),'b.');
 plot(BX(1,:),BX(2,:),'r.');
 plot(CX(1,:),CX(2,:),'r.');
 plot(DX(1,:),DX(2,:),'r.');
 title('Sygrish pinakwn B,-B kai B_anastrofos');
 hold off 
 axis square equal
 
 
 %%%Askhsh 3
 disp('Askhsh 3')
 pause;
 
 A = [-2 3; -3 1 ];
 B = [1 3; 1 1];
 X = sampleUnitSphere(2, 100);
 
 figure(1); clf; hold on;
 plot( X(1,:), X(2,:),'b.');
 K = A;
 for i = 1:6
       AX = K*X;
       plot(AX(1,:),AX(2,:),'r.');
       K = A*A;
 end
 title('Dynameis pinaka A');
 hold off 
 axis square equal
 
 
 figure(2); clf; hold on;
 plot( X(1,:), X(2,:),'b.');
 for i = 1:6
       BX = B*X;
       plot(BX(1,:),BX(2,:),'r.');
       B = B*B;
 end
 title('Dynameis pinaka B');
 hold off 
 axis square equal
 

%  %%%Askhsh 4
 disp('Askhsh 4')
 pause;

A = [1 3 -2; 8 0 1; 2 2 6 ];
B = reshape(1:9,3,3);
X1 = sampleUnitSphere(3,200);
X2 = sampleUnitSphere(3,200);
AX1 = A*X1;
BX2= B*X2;
figure(1); clf; 
hold on; 
plot3( X1(1,:), X1(2,:), X1(3,:),'b.'); view(-60,10)
plot3(AX1(1,:),AX1(2,:),AX1(3,:),'g.'); 
plot(BX2(1,:),BX2(2,:),'k.'); 
drawVector(AX1, 'r.')
drawVector(BX2, 'r.')
hold off; 
axis square equal; grid on;


 %%%Askhsh 5
disp('Askhsh 5')
pause;

x= [1 1 1]';
A = diag([1 2 3]);
norm(A*x)

B = [1 3 -2; 8 0 1; 2 2 6 ];
norm(B*x)

 %%%Askhsh 6
disp('Askhsh 6')
pause;

A = [1 3 -2; 8 0 1; 2 2 6 ];
B = reshape(1:9,3,3);

a = norm(A(:),2);
b = norm(B(:),2);
X = a*b
AB= A*B;
ab= norm(AB(:),2)


%%%orthogwnies provoles
disp('orthogwnies provoles')
pause;

 %%%Askhsh 1
disp('Askhsh 1')
pause;

%estw dianysma b to opoio anhkei sto xwro sthlwn tou pinaka A.
%afou o A exei grammika anexarthtes sthles estw 
% b = [ 1 0 1 ]'
disp('h or8ogwnia provolh P sto xwro sthlwn tou A')
A = [1 0; 0 1; 1 0];
b = [ 1 0 1 ]';
x = inv(A'* A)* A'*b;
A*x

%estw dianysma z to opoio anhkei sto xwro sthlwn tou pinaka B.
%afou o A exei grammika anexarthtes sthles estw 
% z = [ 1 0 1 ]'
disp('h or8ogwnia provolh P sto xwro sthlwn tou B')
B = [1 2; 0 1; 1 0];
z = [ 1 0 1 ]';
y = inv(B'* B)* B'*z;
B*y

A = [1 0; 0 1; 1 0];
a = [1 2 3]'; % Vector to project
figure(1); clf; hold on; 
drawVector(a, 'r.', {'a'}); % Plot the vectors 
P_orth = A*A'; % Orthogonal projector 
a_orth = P_orth *a; % Orthogonal projection 
drawVector(a_orth, 'b.', {'a_{orth}'}) % Plot the projections of vector a
drawSpan(A)
hold off 
view(150, 15)

B = [1 2; 0 1; 1 0];
a = [1 2 3]'; % Vector to project
n = [-.5 6 3]';% Vector for the oblique projection 
figure(1); clf; hold on; 
drawVector(a, 'r.', {'a'}); % Plot the vectors 
drawVector(n, 'g.')
P_orth = B*B'; % Orthogonal projector 
P_oblique = B*[1 0 0;0 1 0]*inv([B n]); % Oblique projector
a_orth = P_orth *a; % Orthogonal projection 
a_oblique = P_oblique*a; % Oblique projection 
drawVector([a_orth, 'b.', {'a_{orth}'}) % Plot the projections of vector a
drawSpan(B)
hold off 
view(150, 15)

 %%%Askhsh 2
disp('Askhsh 2')
pause;

A = [1 0; 0 1; 1 0];
a = [1 2 3]'; % Vector to project
n = [-.5 6 3]';% Vector for the oblique projection 
figure(1); clf; hold on; 
drawVector(a, 'r.', {'a'}); % Plot the vectors 
P_oblique = A*[1 0 0;0 1 0]*inv([A n]); % Oblique projector
a_oblique = P_oblique*a; % Oblique projection 
drawVector([a_oblique], 'b.', {'a_{oblique}'}) % Plot the projections of vector a
drawSpan(A)
hold off 
view(150, 15)

B = [1 2; 0 1; 1 0];
a = [1 2 3]'; % Vector to project
n = [-.5 6 3]';% Vector for the oblique projection 
figure(1); clf; hold on; 
drawVector(a, 'r.', {'a'}); % Plot the vectors 
P_oblique = B*[1 0 0;0 1 0]*inv([B n]); % Oblique projector
a_oblique = P_oblique*a; % Oblique projection 
drawVector([a_oblique], 'b.', {'a_{oblique}'}) % Plot the projections of vector a
drawSpan(B)
hold off 
view(150, 15)

